<template>
  <div>
    <van-nav-bar title="不凡乐购" left-text="返回" left-arrow @click-left="onClickLeft" class="navbar" />
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :address-info="addressInfo"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-default="setdefault"
    />
  </div>
</template>

<script>
import areaList from "../../assets/js/area";
import { Toast } from "vant";
import { detailAction,deleteAction } from "@/api/nav";
import { saveAction } from "@/api/home";
export default {
  data() {
    return {
      areaList,
      addressInfo: {}
    };
  },
  methods: {
    setdefault(value){
      console.log(value);
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    onSave(content) {
      console.log(content);
      this.content = content;
      // console.log(this.$route.query.id);
      saveAction({
        address:
          this.content.province + this.content.city + this.content.county,
        addressId: this.$route.query.id ? this.$route.query.id : "",
        checked: this.content.isDefault,
        detailadress: this.content.addressDetail,
        openId: 17516331686,
        telNumber: this.content.tel,
        userName: this.content.name
      }).then(res => {
        console.log(res);
      });
      Toast("保存地址成功");
      this.$router.push("/cart/address");
    },
    onDelete(content) {
      console.log(content);
      deleteAction({
        id: this.$route.query.id
      }).then(res => {
        console.log(res);
      });
      this.addressInfo = {};
      if (content.areaCode == "" || content.areaCode == "") {
        Toast("您还未输入地址详情");
      } else {
        Toast("删除地址成功");
      }
    }
  },
  created() {
    detailAction({
      id: this.$route.query.id
    }).then(res => {
      console.log(res.data);
      this.addressInfo = res.data;
      this.addressInfo.name = res.data.name;
      this.addressInfo.tel = res.data.mobile;
      this.addressInfo.addressDetail = res.data.address_detail;
    });
  }
};
</script>

<style lang="scss" scoped>
</style>